﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Bll;
using Runda.EShop.Common.Entity;
using Runda.EShop.Common.Component;

namespace Runda.EShop.Web.UserControl
{
    public partial class OrderDetailTerminal : OrderProductControl
    {
        public override string ProductCode
        {
            get
            {
                return ViewState["_ProductCode"].ToString();
            }
            set
            {
                ViewState["_ProductCode"] = value;
            }
        }

        private Guid ProductID
        {
            get
            {
                if (Request.QueryString["ProductID"] == null) return Guid.Empty;
                return new Guid(Request.QueryString["ProductID"]);
            }
        }

        public override List<BizShopCartEntity> ProductList
        {
            get
            {
                List<BizShopCartEntity> list = new List<BizShopCartEntity>();
                foreach (RepeaterItem row in rptProdBatch.Items)
                {
                    DecimalTextBox txtQty = row.FindControl("txtQty") as DecimalTextBox;
                    if (txtQty.Value.HasValue)
                    {
                        Literal ltlBatchCode = row.FindControl("ltlBatchCode") as Literal;
                        Literal ltlDueDate = row.FindControl("ltlDueDate") as Literal;
                        TextBox txtInventory = row.FindControl("txtInventory") as TextBox;
                        TextBox txtRemark = row.FindControl("txtRemark") as TextBox; 

                        BizShopCartEntity entity = new BizShopCartEntity();
                        entity.CustomerID = base.Page.User.Identity.LoginUser.UserID;
                        entity.ProductID = ProductID;
                        entity.ProductCode = ProductCode;
                        entity.Quantity = txtQty.Value.Value;
                        entity.BatchCode = ltlBatchCode.Text;
                        if (!string.IsNullOrEmpty(ltlDueDate.Text))
                        {
                            entity.DueDate = Convert.ToDateTime(ltlDueDate.Text);
                        }
                        entity.Inventory = Convert.ToDecimal(txtInventory.Text);
                        entity.Remark = txtRemark.Text;
                        entity.CreatedAt = DateTime.Now;
                        entity.Deleted = false;

                        list.Add(entity);
                    }
                }

                return list;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string EntCode,CustomerID;
            EntCode = base.Page.User.Identity.LoginUser.EntCode;
            CustomerID = base.Page.User.Identity.LoginUser.CO;
            if (!IsPostBack)
            {
                BasProductBll prodBll = new BasProductBll();
                if (prodBll.GetHoldProductStock(CustomerID,EntCode, ProductCode).Rows.Count > 0)
                { rptProdBatch.DataSource = prodBll.GetHoldProductStock(CustomerID,EntCode, ProductCode); }
                else
                {
                    rptProdBatch.DataSource = prodBll.GetOldestProductInventory(base.Page.User.Identity.LoginUser.CO, ProductCode);
                }
                rptProdBatch.DataBind();
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Load += new EventHandler(Page_Load);
        }
    }
}